const Router = require('koa-router')
const { resDesign } = require('@/utils/dataDesign')
const SystemService = require('@/service/system.service')
const HTTP_CODE = require('@/app/http-code')

const roleRouter = new Router({ prefix: '/system/role' })

roleRouter.get('/list', async (ctx) => {
  try {
    const data = await SystemService.selectRole(ctx.query)
    ctx.body = resDesign({ data })
  } catch (error) {
    console.log(error)
  }
})
roleRouter.get('/all', async (ctx) => {
  try {
    const data = await SystemService.selectRoleAll()
    ctx.body = resDesign({ data })
  } catch (error) {
    console.log(error)
  }
})
roleRouter.post('/insert', async (ctx) => {
  try {
    await SystemService.insertRole(ctx.request.body)
    ctx.body = resDesign()
  } catch (error) {
    console.log(error)
  }
})
roleRouter.get('/routelist', async (ctx) => {
  try {
    const { id } = ctx.query
    const data = await SystemService.selectRoleRoutes(id)
    ctx.body = resDesign({ data })
  } catch (error) {
    console.log(error)
  }
})
roleRouter.post('/update', async (ctx) => {
  try {
    await SystemService.updateRole(ctx.request.body)
    ctx.body = resDesign()
  } catch (error) {
    console.log(error)
  }
})
roleRouter.delete('/delete/:id', async (ctx) => {
  try {
    const flag = await SystemService.deleteRole(ctx.params.id)
    if (flag) {
      ctx.body = resDesign()
    } else {
      ctx.body = resDesign({ code: HTTP_CODE.error, msg: '当前角色正在使用' })
    }
  } catch (error) {
    console.log(error)
  }
})

module.exports = roleRouter
